<?php

class Involved_PurchaseResourceModel extends BaseModel
{
    protected $table = 'purchase_resource';//采购表
    //采购信息列表PC端
    public  function PurchaseResource(){
       // $offset = ($paramObj->page-1) * HostEnum::PAGE_NUM;
        return  DB::table($this->table)
            ->select('purchase_id','title','spec','num','price','place','created','contacts','tell','company','sign','unit','address')
          //  ->skip($offset)->take(HostEnum::PAGE_NUM)
          ->where('state',1)
            ->orderBy('created','desc')
         ->paginate(10);
        //  ->get();
    }
    //采购信息列表api
    public  function PurchaseResourceApi($page){
         $offset = ($page-1) * HostEnum::PAGE_NUM;
        return  DB::table($this->table)
            ->select('purchase_id','title','spec','num','price','place','created','contacts','tell','company','sign','unit','address')
            ->skip($offset)->take(HostEnum::PAGE_NUM)
            ->where('state',1)
            ->orderBy('created','desc')
            ->get();
    }
    //采购信息列表api
    public  function UserPurchaseResourceApi($user_id,$state,$page){
        $offset = ($page-1) * HostEnum::PAGE_NUM;
        if($state == 1){
            return  DB::table($this->table)
                ->select('purchase_id','title','spec','num','price','place','created','contacts','tell','company','sign','unit','address')
                ->skip($offset)->take(HostEnum::PAGE_NUM)
                ->where('user_id',$user_id)
                ->where('state',1)
                ->orderBy('created','desc')
                ->get();
        }elseif($state == 0){
            return  DB::table($this->table)
                ->select('purchase_id','title','spec','num','price','place','created','contacts','tell','company','sign','unit','address')
                ->skip($offset)->take(HostEnum::PAGE_NUM)
                ->where('user_id',$user_id)
                ->where('state',0)
                ->orderBy('created','desc')
                ->get();

        }else{
            return  DB::table($this->table)
                ->select('purchase_id','title','spec','num','price','place','created','contacts','tell','company','sign','unit','address')
                ->skip($offset)->take(HostEnum::PAGE_NUM)
                ->where('user_id',$user_id)
                ->where('state',2)
                ->orderBy('created','desc')
                ->get();
        }

    }

    public  function UserPurchaseResourceCount($user_id,$state){
        if($state == 1){
            return  DB::table($this->table)
                ->select('purchase_id')
                ->where('user_id',$user_id)
                ->where('state',1)
                ->get();
        }elseif($state == 0){
            return  DB::table($this->table)
                ->select('purchase_id')
                ->where('user_id',$user_id)
                ->where('state',0)
                ->get();

        }else{
            return  DB::table($this->table)
                ->select('purchase_id')
                ->where('user_id',$user_id)
                ->where('state',2)
                ->get();
        }

    }
    //个人中心采购信息列表
    public  function PurchaseResourceAdmin(){
        // $offset = ($paramObj->page-1) * HostEnum::PAGE_NUM;
        return  DB::table($this->table)
            ->select('purchase_id','title','spec','num','price','place','created','contacts','tell','company','sign','unit','address','state')
            //  ->skip($offset)->take(HostEnum::PAGE_NUM)
            ->where('user_id',session_get(UserEnum:: USER_INFO_USER_ID))
            ->orderBy('created','desc')
          ->get();
    }
    //个人中心采购列表删除
    public function PurchaseResourceDel($purchase_id){
        return DB::table($this->table)
            ->where(['purchase_id' =>$purchase_id])
            ->delete();
    }
    #采购查询
    public  function SelectPurchase($paramObj){
        $db = DB::table($this->table);
        if (!empty($paramObj->title)) {
            $db->where('title', 'like', "%{$paramObj->title}%");
        }
        if (!empty($paramObj->place)) {
            $db->where('place', 'like', "%{$paramObj->place}%");
        }
        return  $db
            ->select('title','spec','num','price','place','created','contacts','tell','company','sign','unit','address')
            ->where('state',1)
            ->orderBy('created','desc')
            ->paginate(10);
    }
    #发布采购
    public function AddPurchase(array $aData){
        return DB::table($this->table)->insert($aData);
    }

    //首页采购信息列表
    public  function IndexPurchaseResource(){
        // $offset = ($paramObj->page-1) * HostEnum::PAGE_NUM;
        return  DB::table($this->table)
            ->select('title','spec','num','price','place','created','contacts','tell','company','sign','unit','address')
            //  ->skip($offset)->take(HostEnum::PAGE_NUM)
            ->where('state',1)
            ->orderBy('created','desc')
            ->take(7)
            ->get();
    }
    public  function PurchaseResource1(){
        // $offset = ($paramObj->page-1) * HostEnum::PAGE_NUM;
        return  DB::table($this->table)
            ->select('purchase_id','title','spec','num','price','place','created','contacts','tell','company','sign','unit','address','state')
            //  ->skip($offset)->take(HostEnum::PAGE_NUM)
            ->orderBy('created','desc')
          ->get();
    }
    public function PurchaseResourceDetails($purchase_id){
        return  DB::table($this->table)
            ->select('purchase_id','title','spec','num','price','place','created','contacts','tell','company','sign','unit','address','state')
            ->where('purchase_id',$purchase_id)
            ->first();
    }
    public function PurchaseResourceExamine($examine,$purchase_id,$title,$spec,$num,$unit,$price,$place,$contacts,$tell,$company){
        return  DB::table($this->table)
        ->where('purchase_id',$purchase_id)
            ->update(array('state' => $examine,'title'=>$title,'spec'=>$spec,'num'=>$num,'unit'=>$unit,'price'=>$price,'place'=>$place,'contacts'=>$contacts,'tell'=>$tell,'company'=>$company));

    }

    //未审核采购数量
    public function PurchaseResourceNum(){
        return  DB::table($this->table)
            ->select('purchase_id')
            ->where('state',0)
            ->count();

    }
}
